from flask import Blueprint,request,jsonify
import json
from db import db1
from utils.redisc import red
bp_work = Blueprint('workflow',__name__)

@bp_work.route('worklist')
def worklist():
    sql = "select * from workflow"
    res = db1.findall(sql)
    return  jsonify({'code':200,'list':res})

# 获取数据源
@bp_work.route('getsourcedata')
def getsourcedata():
    # 从redis取，如果有直接返回
    values = red.find_set('dataresouce')
    if values:
        list = eval(values)
    else:
        sql="select * from data_source"
        res = db1.findall(sql)
        list=[]
        if res:
            for i in res:
                dict = {'id':i['id'],'name':i['name']}
                sql1 = "select * from %s"%(i['tablename'])
                subres = db1.findall(sql1)
                dict['mes']=subres
                list.append(dict)
                print(list)
        red.insert_set('dataresouce',json.dumps(list))
    return jsonify({'code':200,'list':list})
